/**
 */
package com.dingpeikeji.quickdeer.core.modules.sys.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.TreeService;
import com.dingpeikeji.quickdeer.core.common.utils.CacheUtils;
import com.dingpeikeji.quickdeer.core.modules.sys.dao.OfficeDao;
import com.dingpeikeji.quickdeer.core.modules.sys.dao.UserDao;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Area;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.google.common.collect.Lists;
import com.mysql.jdbc.StringUtils;

/**
 * 机构Service
 * @version 2014-05-16
 */
@Service
@Transactional(readOnly = true)
public class OfficeService extends TreeService<OfficeDao, Office> {

	@Autowired
	private AreaService areaService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private OfficeDao officeDao;
	
	
	public List<Office> findAll(){
		return UserUtils.getOfficeList();
	}

	public List<Office> findList(Boolean isAll){
		if (isAll != null && isAll){
			return UserUtils.getOfficeAllList();
		}else{
			return UserUtils.getOfficeList();
		}
	}
	
	public List<Office> findListUnselect(String flatType){
		//获取当前用户所属公司
		Office office = UserUtils.getUser().getOffice();
		office.setFlatType(flatType);
		
		return dao.findListUnselect(office);
	}
	/**
	 * 根据公司名获取公司
	 * @param name
	 * @return
	 */
	public Office getOfficeByOfficeName(String officeName, String flatType)
	{
		if (!officeName.isEmpty() && !flatType.isEmpty()) {
			return dao.findOfficeByName(officeName, flatType);
		}
		return	null;
	}
	
	/**
	 * 根据城市获取公司
	 * @param name
	 * @return
	 */
	public List<Office> getOfficeByCitys(String citys)
	{
		//传入
		String cityid = UserUtils.getUser().getOffice().getArea().getId();
		Area area = areaService.get(cityid);
		String code = area.getCode();
		List<String> cityList = Lists.newArrayList();
		if (code != null){
			cityList.add(code);
			String city[] = citys.split(",");
			for(String c : city){
				if(!StringUtils.isNullOrEmpty(c)){
					cityList.add(c);
				}
			}
		}

		if (!citys.isEmpty() && !citys.isEmpty()) {
			List<Office> officelist = dao.getOfficeByCitys(cityList);
			return	officelist;
		}
		return	null;
	}
	/**
	 * 根据组织机构代码获取公司
	 * @param orgcode
	 * @return
	 */
	public Office getOfficeByOrgcode(String orgcode, String flatType)
	{
		if (!orgcode.isEmpty() && !flatType.isEmpty()) {
			return dao.findOfficeByOrgCode(orgcode, flatType);
		}
		return	null;
	}
	/**
	 * 根据营业执照获取公司
	 * @param orgcode
	 * @return
	 */
	public Office getOfficeByLicenseno(String licenseno, String flatType)
	{
		if (!licenseno.isEmpty() && !flatType.isEmpty()) {
			return dao.findOfficeByLicenseno(licenseno, flatType);
		}
		return	null;
	}
	/**
	 * 根据税务登记证获取公司
	 * @param taxregno
	 * @return
	 */
	public Office getOfficeByTaxregno(String taxregno, String flatType)
	{
		if (!taxregno.isEmpty() && !flatType.isEmpty()) {
			return dao.findOfficeByTaxregno(taxregno, flatType);
		}
		return	null;
	}
	@Transactional(readOnly = true)
	public List<Office> findList(Office office){
		if(office != null){
			office.setParentIds(office.getParentIds()+"%");
			return dao.findByParentIdsLike(office);
		}
		return  new ArrayList<Office>();
	}

	public List<Office> findListByTerm(Office office){
		office.setUseable(Office.CAN_USE);
		office.setParentIds(office.getParentIds()+"%");
			return dao.findList(office);
	}
	
	public Page<Office> findPage(Page<Office> page, Office office) {
		return super.findPage(page, office);
	}
	@Transactional(readOnly = false)
	public void save(Office office) {
		super.save(office);
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}
	
	@Transactional(readOnly = false)
	public void update(Office office) {
		super.update(office);
		User user = new User();
		user.setOffice(office);
		List<User> userList = userDao.findList(user);
		if(userList!=null){
			for(User temp : userList) {
				UserUtils.clearCache(temp);
			}
 		}
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}
	
	/**
	 * 直接插入到数据库不作任何处理
	 * @param office
	 */
	@Transactional(readOnly = false)
	public void savef(Office office) {
		dao.insert(office);
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}
	
	@Transactional(readOnly = false)
	public void delete(Office office) {
		super.delete(office);
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}
	
	/**
	 * 根据平台类型获取该平台的ID
	 * @param flatType
	 * @return
	 */
	public Office findParentByFlatType(String flatType) {
		return dao.findParentByFlatType(flatType);
	}
	
	public List<Office> findListByAreaid(String flatType, String area_id ){
		if(flatType != null && area_id != null){
			return dao.findListByAreaid(flatType, area_id);
		}
		return  new ArrayList<Office>();
	}

	public String findAreaNameByRentproduct(TRentproduct tRentproduct) {
		return areaService.findAreaNameByRentproduct(tRentproduct);
	}

	public String findIdByNameAndAbbr(String name, String abbr) {
		return officeDao.findIdByNameAndAbbr(name,abbr);
	}

	public String findIdByAbbr(String parameter) {
		return officeDao.findIdByAbbr(parameter);
	}
	
}
